package main

import (
	"fmt"
	"goYmodem"
	"goYmodem/utils"
	"os"
)

type myIO struct {
}

func (m myIO) Read(p []byte) (n int, err error) {
	return 0, nil
}

func (m myIO) Write(p []byte) (n int, err error) {
	return 1, nil
}

var f *os.File

func main() {
	y := &goYmodem.Ymodem{
		ReadWriter: myIO{},
	}
	y.BeginPack = func(data []byte) {
		file := y.FileName
		fileexist, _ := utils.PathExists(file)
		if fileexist != true {
			f, _ = os.Create(file)
		} else {
			f, _ = os.OpenFile(file, os.O_APPEND, 0666)
		}
	}
	y.DataPack = func(data []byte) {
		n, err := f.Write(data)
		if err != nil {
			panic(err)
		}
		fmt.Println("write ", n, " data")
	}
	y.EndPack = func(data []byte) {
		err := f.Close()
		if err != nil {
			return
		}
	}
	y.Receive()
}
